package cn.mesmile.product.controller;

import cn.mesmile.product.entity.Product;
import cn.mesmile.product.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zb
 * @date 2020/12/16 14:59
 * @Description
 */
@AllArgsConstructor
@RequestMapping("/api/v1/product")
@RestController
public class ProductController {

    private final ProductService productService;

    @GetMapping("/get")
    public String get() {
        return "获取到剩余产品数量为： 1";
    }


    @GetMapping("/getProduct/{productId}")
    public Product getProduct(@PathVariable("productId")Long productId){
        LambdaQueryWrapper<Product> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(productId != null, Product::getId, productId);
        return productService.getOne(lambdaQueryWrapper);
    }

    @GetMapping("/update/{productId}/{number}")
    public Boolean update(@PathVariable("productId")Long productId,@PathVariable("number")Long number){
        LambdaUpdateWrapper<Product> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(productId != null,Product::getId,productId);
        lambdaUpdateWrapper.set(number != null,Product::getStock,number);
        return productService.update(lambdaUpdateWrapper);
    }


}
